<?php 
/***
	Jpublic.1.0.php
	name:xiaojia
	email:iatt@qq.com
	qq:273142650
	time:2011.9.4 13:03
***/

	class Jpublic {
		
		/*		
		 * POST GET REQUEST
		 * Jpublic::post,get,request ($string, $html, $get_magic_quotes, $start, $length, $encode);
		 * $string          : '必选，需要获取的值名称';
		 * $html            : '可选，是否对HTML标记进行处理，0为不做处理，1为去除所有HTML，2为对HTML进行转义，默认为0';
		 * $get_magic_quotes: '可选，是否进行PHP默认转义处理，0为系统默认，1为对没有转义的字符串进行转义，2为对转义过的字符串进行还原，默认为1';
		 * $start           : '可选，字符串截取，详情参见mb_substr()，默认不作为;
		 * $length          : '可选，字符串截取，详情参见mb_substr()，默认不作为';
		 * $encode          : '可选，字符串截取编码，详情参见mb_substr()，默认utf-8编码';
		*/

		static $int_str;
		
		//POST方法
		static public function post ($str, $html = 0, $gpc = 1, $start = 0, $length = 0, $encode = 'utf-8')
		{
			
			if (in_array($str, self::$int_str)) {
			
				$p = (int)urldecode(trim($_POST[$str]));
			
			} else {
			
				$p = urldecode(trim($_POST[$str]));
			
			}
			
			$p = self::encode($p, $html, $gpc, $start, $length, $encode);			
			
			return $p;
			
		}//post end
		
		//GET方法
		static public function get ($str, $html = 0, $gpc = 1, $start = 0, $length = 0, $encode = 'utf-8')
		{
			
			if (in_array($str, self::$int_str)) {
			
				$p = (int)urldecode(trim($_GET[$str]));
			
			} else {
			
				$p = urldecode(trim($_GET[$str]));
			
			}
			
			$p = self::encode($p, $html, $gpc, $start, $length, $encode);	
			
			return $p;
			
		}//get end
		
		
		//request方法
		static public function request ($str, $html = 0, $gpc = 1, $start = 0, $length = 0, $encode = 'utf-8')
		{
			$p = trim($_REQUEST[$str]);
			
			$p = self::encode($p, $html, $gpc, $start, $length, $encode);	
			
			return $p;
			
		}//request end
		
		static public function encode ($p, $html = 0, $gpc = 0, $start = 0, $length = 0, $encode = 'utf-8')
		{
			//HTML转义
			switch ($html) {
				case 1:
					$p = self::clear_html($p);
				break;
				case 2:
					$p = self::HTML2Encode($p);
				break;
			}
			
			//截断
			$p = $length == 0 ? $p : mb_substr($p, $start, $length, $encode);
			
			//符号转意
			if ( $gpc == 1 ) {
				$p = self::add_slashes($p);
			} elseif( $gpc == 2) {
				$p = self::strip_slashes($p);
			}
			
			return $p;
		}
		
		static public function request_post($data, $target) {
		
			$url_info = parse_url($target);
		
			$httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
		
			$httpheader .= "Host:" . $url_info['host'] . "\r\n";
		
			$httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n";
		
			$httpheader .= "Content-Length:" . strlen($data) . "\r\n";
		
			$httpheader .= "Connection:close\r\n\r\n";
		
			$httpheader .= $data;		
		
			$fd = fsockopen($url_info['host'], 80);
		
			fwrite($fd, $httpheader);
		
			$gets = "";
		
			while(!feof($fd)) {
		
				$gets .= fread($fd, 128);
		
			}
		
			fclose($fd);
		
			return $gets;
		
		}
		
		/*		
		 * Jpublic::clear_html ($string)          : '清除HTML标记';
		 * Jpublic::add_slashes ($string)         : 'PHP转义';
		 * Jpublic::strip_slashes ($string)       : '清除PHP转义';
		 * Jpublic::HTML2Encode ($string)         : '对 &, <, >, \n 进行转义';
		 * Jpublic::Encode2HTML ($string)         : 'HTML2Encode的还原';
		 * Jpublic::alert($string, $url, $parent) : '$string 必选，弹出的信息警告，如果为空则不提示。$url 可选，如果不为空则是一个URL，进行跳转页。$parent 跳转的级别，默认为本页self，URL不为空才执行';
		*/
		
		static public function clear_html ($str)
		{
			return preg_replace('/<[^>]*>/', '', $str);
		}
		
		static public function add_slashes ($str)
		{
			return get_magic_quotes_gpc() == 0 ? addslashes($str) : $str;
			
		}
		
		static public function strip_slashes ($str)
		{
			return get_magic_quotes_gpc() == 1 ? stripslashes($str) : $str;
			
		}
		
		//TEXTAREA替换标签
		static public function HTML2Encode ($str)
		{
			$str = str_replace("&","&amp;",$str);
			$str = str_replace("<","&lt;",$str);
			$str = str_replace(">","&gt;",$str);
			$str = str_replace("\n","<br />",$str);
			return $str;
		
		}
		
		//TEXTAREA替换标签
		static public function Encode2HTML ($str)
		{
			$str = str_replace("&amp;","&",$str);
			$str = str_replace("&lt;","<",$str);
			$str = str_replace("&gt;",">",$str);
			$str = str_replace("<br />","\n",$str);
			return $str;
		}
		
		//alert
		static public function alert ($str, $url = '', $p = 'self')
		{
			$location = $url == '' ? '' : $p.".location.href = '$url' + top.window.location.hash;";
			$alert = $str == '' ? '' : "alert('$str');";
			echo "<script>$alert$location</script>";
			exit();
		}
		
		static public function login ()
		{
			echo "<script>top.jPushInit.login();</script>";
			exit();
		}
		
		//location
		static public function location ($href)
		{
			header("Location: $href");
			exit();
		}
		
		static public function all_get ($remove = array())
		{
			$get = $_GET;
			foreach ($get as $key => $v) {
				if (!in_array($key, $remove)) {
					$field[] = $key .'='. $v;
				}
			}
			
			return implode('&', $field);
		}
		
	}//class end
?>